package com.example.mystore.controller;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.example.mystore.config.AlipayConfig;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class PayController {

    @RequestMapping("/toPay")
    @ResponseBody
    public String toPay(String orderid, String price, String title) {
        // 获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id,
                AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key,
                AlipayConfig.sign_type);

        // 设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        try {
            // 发送请求参数 orderid 订单id，price表示价格，title表示订单标题
            alipayRequest.setBizContent("{\"out_trade_no\":\"" + orderid + "\","
                    + "\"total_amount\":\"" + price + "\","
                    + "\"subject\":\"" + title + "\","
                    + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

            // 请求
            String form = alipayClient.pageExecute(alipayRequest).getBody();

            // 构建完整的HTML页面返回
            String html = "<!DOCTYPE html>" +
                    "<html lang=\"en\">" +
                    "<head>" +
                    "<meta charset=\"UTF-8\">" +
                    "<title>支付宝支付</title>" +
                    "</head>" +
                    "<body>" +
                    form +
                    "</body>" +
                    "</html>";

            System.out.println("*********************\n返回结果为：" + html);
            return html;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
